package com.bianguo.android.baidumapdemo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;

import java.net.URISyntaxException;

/**
 * Created by Administrator on 2018/2/28.
 */

public class LocationHelper {

    private static LocationHelper mHelper;
    private Context mContext;
    private LocationClient mClient;

    private LocationHelper(Context mContext) {
        this.mContext = mContext;
    }

    public static LocationHelper getInstance(Context mContext) {
        synchronized (LocationHelper.class) {
            if (mHelper == null) {
                synchronized (LocationHelper.class) {
                    if (mHelper == null) {
                        mHelper = new LocationHelper(mContext);
                    }
                }
            }
        }
        return mHelper;
    }

    public void startLocation(BDAbstractLocationListener listener) {
        mClient = new LocationClient(mContext);
        mClient.registerLocationListener(listener);
        LocationClientOption option = new LocationClientOption();

        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //可选，设置定位模式，默认高精度
        //LocationMode.Hight_Accuracy：高精度；
        //LocationMode. Battery_Saving：低功耗；
        //LocationMode. Device_Sensors：仅使用设备；

        option.setCoorType("bd09ll");
        //可选，设置返回经纬度坐标类型，默认gcj02
        //gcj02：国测局坐标；
        //bd09ll：百度经纬度坐标；
        //bd09：百度墨卡托坐标；
        //海外地区定位，无需设置坐标类型，统一返回wgs84类型坐标

        option.setScanSpan(1000);
        //可选，设置发起定位请求的间隔，int类型，单位ms
        //如果设置为0，则代表单次定位，即仅定位一次，默认为0
        //如果设置非0，需设置1000ms以上才有效

        option.setOpenGps(true);
        //可选，设置是否使用gps，默认false
        //使用高精度和仅用设备两种定位模式的，参数必须设置为true

        option.setLocationNotify(true);
        //可选，设置是否当GPS有效时按照1S/1次频率输出GPS结果，默认false

        option.setIgnoreKillProcess(false);
        //可选，定位SDK内部是一个service，并放到了独立进程。
        //设置是否在stop的时候杀死这个进程，默认（建议）不杀死，即setIgnoreKillProcess(true)

        option.SetIgnoreCacheException(false);
        //可选，设置是否收集Crash信息，默认收集，即参数为false

        option.setWifiCacheTimeOut(5 * 60 * 1000);
        //可选，7.2版本新增能力
        //如果设置了该接口，首次启动定位时，会先判断当前WiFi是否超出有效期，若超出有效期，会先重新扫描WiFi，然后定位

        option.setEnableSimulateGps(false);
        //可选，设置是否需要过滤GPS仿真结果，默认需要，即参数为false

        option.setIsNeedAddress(true);

        mClient.setLocOption(option);
        //mLocationClient为第二步初始化过的LocationClient对象
        //需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
        //更多LocationClientOption的配置，请参照类参考中LocationClientOption类的详细说明
        mClient.start();
    }

    public void stopLocation() {
        if (mClient != null) {
            mClient.stop();
            mClient = null;
        }
    }

    public void lunchForMapApp(Activity mContext, int tag, LatLng sLatLng, LatLng eLatLng, String sName, String eName) {
        if (PhoneUtil.checkAppExist(mContext, "com.autonavi.minimap")) {//高德地图导航
            String strName = null;
            if (tag == 0) {
                strName = "&t=0";
            } else if (tag == 1) {
                strName = "&t=1";
            } else if (tag == 2) {
                strName = "&t=2";
            }
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setPackage("com.autonavi.minimap");
            intent.addCategory("android.intent.category.DEFAULT");
            Uri uri = Uri
                    .parse("androidamap://route?sourceApplication=softname&slat="
                            + sLatLng.latitude + "&slon=" + sLatLng.longitude + "&dlat=" + eLatLng.latitude + "&dlon=" + eLatLng.longitude
                            + "&dname=" + eName + "&dev=0&m=1" + strName);
            intent.setData(uri);
            mContext.startActivity(intent);
        } else if (PhoneUtil.checkAppExist(mContext, "com.baidu.BaiduMap")) {//百度地图导航
            String strName = null;
            if (tag == 0) {
                strName = "transit";
            } else if (tag == 1) {
                strName = "driving";
            } else if (tag == 2) {
                strName = "walking";
            }
            String baiduUrl = "intent://map/" +
                    "direction?origin=latlng:" + sLatLng.latitude + "," +
                    sLatLng.longitude + "|name:" + sName + "&destination=" + eName + "&mode=" + strName + "&src=fayuan" +
                    "#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end";
            try {
                Intent intent = Intent.parseUri(baiduUrl, 0);
                mContext.startActivity(intent); //启动调用
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(mContext, "请安装高德地图或百度地图后再次操作！", Toast.LENGTH_SHORT).show();
        }
    }

    private void DaoHang(Activity mContext, int tag, String str, String s1, String s2) {
        Intent intent = null;
        String strName = null;
        if (tag == 0) {
            strName = "transit";
        } else if (tag == 1) {
            strName = "driving";
        } else if (tag == 2) {
            strName = "walking";
        }
        try {
            intent = Intent.getIntent("intent://map/" +
                    "direction?origin=latlng:" + 39.915291 + "," +
                    116.403857 + "|name:你家，他家" + "&destination=" + "百度大厦" + "&mode=" + strName + "&src=fayuan" +
                    "#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        mContext.startActivity(intent); //启动调用
    }
}
